package com.paireach.gridviewheaderwithfooter01;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import in.srain.cube.views.GridViewWithHeaderAndFooter;

public class MainActivity extends AppCompatActivity {

    private Context context = this;
    private GridViewWithHeaderAndFooter gridVivew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridVivew = (GridViewWithHeaderAndFooter) findViewById(R.id.gridView);


        View headerView1 = LayoutInflater.from(context).inflate(R.layout.header_1, gridVivew, false);
        View headerView2 = LayoutInflater.from(context).inflate(R.layout.header_2, gridVivew, false);
        View footerView1 = LayoutInflater.from(context).inflate(R.layout.footer_1, gridVivew, false);
        View footerView2 = LayoutInflater.from(context).inflate(R.layout.footer_2, gridVivew, false);

        //addHeaderView()+addFooterView()必须在setAdapter（）前
        gridVivew.addHeaderView(headerView1);
        gridVivew.addHeaderView(headerView2);
        gridVivew.addFooterView(footerView1);
        gridVivew.addFooterView(footerView2);

        MyGridViewAdapter adapter = new MyGridViewAdapter();
        gridVivew.setAdapter(adapter);
        gridVivew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(context, provinces[position] + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    private String[] provinces;

    public class MyGridViewAdapter extends BaseAdapter {


        public MyGridViewAdapter() {
            super();
            provinces = getResources().getStringArray(R.array.province);
        }

        @Override
        public int getCount() {
            return provinces.length;
        }

        @Override
        public Object getItem(int position) {
            return provinces[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, parent, false);
                holder = new ViewHolder();
                holder.tv = (TextView) convertView.findViewById(R.id.tv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv.setText(provinces[position]);
            return convertView;
        }
    }

    public static class ViewHolder {
        public TextView tv;
    }
}
